package com.example.u8test;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.example.u8test.Utils.U8;
import com.example.u8test.gson.LoginResponse;
import com.google.gson.Gson;

import okhttp3.RequestBody;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;


public class LoginActivity extends BaseActivity {

    private EditText etUserName;
    private EditText etPassword;
    private CheckBox cbRememberPassword;
    private Button btnLogin;

    private String TAG = "LoginActivity";

    private String from_account = "pc_yonyou";
    private String to_account = "test_pc_yonyou";
    private String app_key = "opa205ef9dc127a0704";
    private String app_secret = "352725e0913b40e5989cbf9dad5c4be4";

    private U8 u8;

    private LinearLayout llLoading;
    private GifImageView gifImageView;
    private GifDrawable gifDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        load();
        u8 = new U8(this, from_account, to_account, app_key, app_secret);
        setListener();
    }

    private void initView() {

        etPassword = findViewById(R.id.et_password);
        etUserName = findViewById(R.id.et_username);
        cbRememberPassword = findViewById(R.id.cb_remember_password);
        btnLogin = findViewById(R.id.bt_go);
        gifImageView = findViewById(R.id.gifImageView);
        gifDrawable = (GifDrawable) gifImageView.getDrawable();
        llLoading = findViewById(R.id.ll_loading);

    }

    private void setListener() {
        btnLogin.setOnClickListener(v -> {
            if (checkInput()) {
                login();
            }
        });
    }


    private Boolean checkInput() {
        if (etUserName.getText().toString().isEmpty()) {
            showNotice("请输入账号！");
            return false;
        }
        if (etPassword.getText().toString().isEmpty()) {
            showNotice("请输入密码！");
            return false;
        }

        if (cbRememberPassword.isChecked()) {
            save();
        }
        return true;
    }

    private void showNotice(String s) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LoginActivity.this, s, Toast.LENGTH_LONG).show();
            }
        });

    }

    /**
     * 加载用户账号，密码等
     */
    private void load() {

        SharedPreferences spf = getSharedPreferences("data", MODE_PRIVATE);
        boolean bRemember = spf.getBoolean("remember_password", false);
        Log.d("bRemember", "load: " + bRemember);
        if (bRemember) {
            cbRememberPassword.setChecked(true);
            String username = spf.getString("username", "");
            String password = spf.getString("password", "");
//            Log.d("数据读取", "load: username " + username + "  password " + password);
            etUserName.setText(username);
            etUserName.setSelection(username.length());
            etPassword.setText(password);
        } else {
            cbRememberPassword.setChecked(false);
        }
    }

    private void save() {

        SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();

        if (cbRememberPassword.isChecked()) {

            editor.putString("username", etUserName.getText().toString());
            editor.putString("password", etPassword.getText().toString());
        }
        editor.putBoolean("remember_password", cbRememberPassword.isChecked());
        editor.apply();
    }


    private void login() {
        showLoading();
        if (checkInput()) {
            u8.setU8LoginCallBack(new U8.U8LoginCallBack() {
                @Override
                public void callBack(String responseJson) {

//                    showNotice(responseJson);
                    Gson gson = new Gson();
                    LoginResponse loginResponse = gson.fromJson(responseJson, LoginResponse.class);
                    if (loginResponse.getErrcode().equals("0")) {
                        hideLoading();
                        showNotice("登陆成功！");
                    } else {
                        showNotice(loginResponse.getErrmsg());
                    }
                }
            });
            u8.login_v2(etUserName.getText().toString(), etPassword.getText().toString());
        }
    }


    private void showLoading() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                llLoading.setVisibility(View.VISIBLE);
                gifDrawable.start();
                gifDrawable.setLoopCount(0);
            }
        });

    }

    private void hideLoading() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                llLoading.setVisibility(View.GONE);
            }
        });

    }


}




